var t = require("../../../@babel/runtime/helpers/interopRequireDefault")(require("we-cropper/we-cropper.js")), e = (getApp().globalData.config, 
wx.getSystemInfoSync()), a = e.windowWidth;

Page({
    data: {
        w: 130,
        h: 189,
        msg: "",
        bgcolor: "rgb(67,142,219)",
        sxContent: "选择照片",
        cuthidden: !0,
        percent: 0,
        progress: !0,
        buttonhidden: !1,
        buttons: [ {
            id: 1,
            name: "蓝色"
        }, {
            id: 2,
            name: "白色"
        }, {
            id: 3,
            name: "红色"
        } ],
        buttons1: [ {
            id: 1,
            name1: "小一寸"
        }, {
            id: 2,
            name1: "一  寸"
        }, {
            id: 3,
            name1: "大一寸"
        }, {
            id: 4,
            name1: "小二寸"
        }, {
            id: 5,
            name1: "二  寸"
        } ],
        cropperOpt: {
            id: "cropper",
            targetId: "targetCropper",
            pixelRatio: e.pixelRatio,
            width: a,
            height: 350,
            scale: 2.5,
            zoom: 8,
            bgcolor: "rgb(67,142,219)",
            cut: {
                x: (a - 200) / 2,
                y: 25,
                width: 300,
                height: 300
            },
            boundStyle: {
                mask: "rgba(255,255,255)",
                lineWidth: 0
            }
        }
    },
    touchStart: function(t) {
        this.cropper.touchStart(t);
    },
    touchMove: function(t) {
        this.cropper.touchMove(t);
    },
    touchEnd: function(t) {
        this.cropper.touchEnd(t);
    },
    bindViewTap: function() {
        var o = this, n = this, i = this, s = i.data.w, c = i.data.h, r = i.data.bgcolor, d = {
            id: "cropper",
            targetId: "targetCropper",
            pixelRatio: e.pixelRatio,
            width: a,
            height: 350,
            scale: 2.5,
            zoom: 8,
            bgcolor: r,
            cut: {
                x: (a - s) / 2,
                y: (350 - c) / 2,
                width: s,
                height: c
            },
            boundStyle: {
                lineWidth: 0
            }
        };
        this.cropper = new t.default(d).on("ready", function(t) {}).on("beforeImageLoad", function(t) {
            o.loadModal.loadModal(!0, "上传中..");
        }).on("imageLoad", function(t) {
            o.loadModal.loadModal(!1);
        }), wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                i.setData({
                    progress: !1,
                    sxContent: "处理中...",
                    percent: 98
                }), wx.getFileSystemManager().readFile({
                    filePath: t.tempFilePaths[0],
                    encoding: "base64",
                    success: function(t) {
                        var e = t.data;
                        wx.request({
                            method: "POST",
                            url: "https://aip.baidubce.com/oauth/2.0/token?charset=UTF-8&grant_type=client_credentials&client_id=cYeO3KN7C7SfFgaCEw9CUf0I&client_secret=qBpq95OvfIv5cO0q0OGsIgn6CvOXxTqG",
                            data: {},
                            success: function(t) {
                                var a = t.data.access_token;
                                wx.request({
                                    method: "POST",
                                    url: "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg?charset=UTF-8&access_token=" + a,
                                    data: {
                                        image: e
                                    },
                                    header: {
                                        "Content-type": "application/x-www-form-urlencoded"
                                    },
                                    success: function(t) {
                                        var e = wx.getFileSystemManager(), a = new Date().getTime(), o = wx.env.USER_DATA_PATH + "/" + a + ".png";
                                        e.writeFile({
                                            filePath: o,
                                            data: t.data.foreground,
                                            encoding: "base64",
                                            success: function(t) {
                                                n.cropper.pushOrign(o), i.setData({
                                                    cuthidden: !1,
                                                    buttonhidden: !0,
                                                    percent: 100
                                                });
                                            }
                                        });
                                    },
                                    fail: function(t) {
                                        wx.showModal({
                                            showCancel: !1,
                                            confirmText: "好的",
                                            title: "生成失败",
                                            content: "再来一次"
                                        });
                                    }
                                });
                            },
                            fail: function(t) {
                                wx.showModal({
                                    showCancel: !1,
                                    confirmText: "好的",
                                    title: "生成失败",
                                    content: "再来一次"
                                });
                            }
                        });
                    },
                    fail: function(t) {
                        console.log(t);
                    }
                });
            },
            fail: function(t) {
                wx.showModal({
                    showCancel: !1,
                    confirmText: "好的",
                    title: "生成失败",
                    content: "再来一次"
                });
            }
        });
    },
    radioButtonTap: function(t) {
        for (var e = t.currentTarget.dataset.id, a = 0; a < this.data.buttons.length; a++) this.data.buttons[a].id == e ? this.data.buttons[a].checked = !0 : this.data.buttons[a].checked = !1;
        this.setData({
            buttons: this.data.buttons,
            msg: "id:" + e
        }), "1" == e && this.setData({
            bgcolor: "rgb(67,142,219)"
        }), "2" == e && this.setData({
            bgcolor: "rgb(255,255,255)"
        }), "3" == e && this.setData({
            bgcolor: "rgb(255,0,0)"
        });
    },
    radioButtonTap1: function(t) {
        for (var e = t.currentTarget.dataset.id, a = 0; a < this.data.buttons1.length; a++) this.data.buttons1[a].id == e ? this.data.buttons1[a].checked1 = !0 : this.data.buttons1[a].checked1 = !1;
        this.setData({
            buttons1: this.data.buttons1,
            msg: "id:" + e
        }), "1" == e && this.setData({
            w: 130,
            h: 189
        }), "2" == e && this.setData({
            w: 148,
            h: 207
        }), "3" == e && this.setData({
            w: 195,
            h: 283
        }), "4" == e && this.setData({
            w: 207,
            h: 266
        }), "5" == e && this.setData({
            w: 207,
            h: 289
        });
    },
    getCropperImage: function() {
        this.cropper.getCropperImage().then(function(t) {
            wx.previewImage({
                current: "",
                urls: [ t ]
            });
        }).catch(function(t) {
            wx.showModal({
                title: "温馨提示",
                content: t.message
            });
        });
    },
    saveCropperImage: function() {
        var t = this;
        this.cropper.getCropperImage().then(function(e) {
            wx.saveImageToPhotosAlbum({
                filePath: e,
                success: function(t) {
                    wx.showModal({
                        showCancel: !1,
                        confirmText: "好的",
                        title: "保存成功"
                    });
                }.bind(t),
                fail: function(t) {
                    wx.showModal({
                        showCancel: !1,
                        confirmText: "好的",
                        title: "保存失败"
                    });
                }.bind(t),
                complete: function(t) {}
            });
        }).catch(function(t) {
            wx.showModal({
                title: "温馨提示",
                content: t.message
            });
        });
    },
    onLoad: function(t) {
        this.loadModal = this.selectComponent("#loadModal"), this.data.buttons[0].checked = !0, 
        this.data.buttons1[0].checked1 = !0, this.setData({
            buttons: this.data.buttons,
            buttons1: this.data.buttons1
        });
    },
    onShareAppMessage: function() {},
    onShareTimeline: function() {}
});